#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#       htmlCompressor.py
#       
#       Copyright 2010 bl4de <bl4d393@gmail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import sys, os
import tarfile

"""htmlCompressor(path, remove=False)
        archivia in un file tar il file specificato in path e la cartella di files(se esiste).
        se remove è True, cancella il file e la cartella originali"""
class htmlCompressor:
    def __init__(self, path, remove=False):
        if os.path.isfile(path):
            self.__pack(path, remove)
        else:
            print 'Il file "%s" non esiste.' % (path,)

    def __pack(self, path_file_html, remove):
        (self.path, self.filename) = os.path.split(path_file_html)
        self.filename_noext = os.path.splitext(self.filename)[0]
        self.path_file_tar = os.path.join(self.path, self.filename_noext+".tar")
        
        self.dir_files = self.filename_noext+"_files"
        self.dir_files1 = "File di "+self.filename_noext
        self.path_dir_files = os.path.join(self.path, self.dir_files)
        self.path_dir_files1 = os.path.join(self.path, self.dir_files1)

        try:
            print "Sto creando il file %s..." % (self.path_file_tar,)
            tarsock = tarfile.TarFile(self.path_file_tar, 'w')
        except tarfile.TarError:
            print tarfile.TarError.message
        else:
            print "File creato"
            print "Aggiungo il file %s all'archivio...\n" % (path_file_html,)
            tarsock.add(path_file_html, self.filename)
            if os.path.isdir(self.dir_files):
                print "Aggiungo la cartella %s all'archivio...\n" % (self.dir_files,)
                tarsock.add(self.path_dir_files, self.dir_files)
            elif os.path.isdir(self.dir_files1):
                print "Aggiungo la cartella %s all'archivio...\n" % (self.dir_files1,)
                tarsock.add(self.path_dir_files1, self.dir_files1)
            tarsock.close()
            
            if remove==True:
                if os.path.isdir(self.dir_files):
                    print "Rimuovo la cartella %s...\n" % (self.dir_files,)
                    self.rmrecdirs(self.dir_files)
                elif os.path.isdir(self.dir_files1):
                    print "Rimuovo la cartella %s...\n" % (self.dir_files1,)
                    self.rmrecdirs(self.dir_files1)
                print "Rimuovo il file %s...\n" % (path_file_html,)
                os.remove(path_file_html)
                
    """rmrecdirs(self, directory)
            rimuove la cartella specificata in directory, tutte le sue sotto-cartelle e i suoi files."""
    def rmrecdirs(self, directory):
        for root, dirs, files in os.walk(directory, topdown=False):
            for name in files:
                os.remove(os.path.join(root, name))
            for name in dirs:
                os.rmdir(os.path.join(root, name))
        os.rmdir(directory)

def main(argv=sys.argv):
    if len(argv) == 3:
        if argv[2]=='-r':
            flag = True
        hc = htmlCompressor(argv[1], flag)
    if len(argv) == 2:
        hc = htmlCompressor(argv[1])
    if len(argv) < 2:
        print "Non ci sono abbastanza argomenti. htmlCompressor.py <filename> [-r]"
    if len(argv) > 3:
        print "Ci sono troppi argomenti. htmlCompressor.py <filename> [-r]"

if __name__ == "__main__":
    main()
